package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserInfoParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStatisticsVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStatisticsVORedisService statisticsVORedisService;

    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        Page<Strategy> page = strategyService.queryForApi(qo);
        return JsonResult.success(page);
    }

    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        // 加载页面的时候阅读数添加
        statisticsVORedisService.addViewnum(id,1);
        return JsonResult.success(strategy);
    }

    @GetMapping("/themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }

    @RequiredLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserInfoParam UserInfo userInfo){
        //copy用户字段到commit对象中
        BeanUtils.copyProperties(userInfo, comment);
        //设置对应的用户id
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    @RequiredLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserInfoParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid, userInfo);
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(statisticsVORedisService.getStatisticsVOInRedis(sid));
    }

    @RequiredLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserInfoParam UserInfo userInfo){
        boolean ret = statisticsVORedisService.isAddFavor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

    @PostMapping("/strategyThumbup")
    @RequiredLogin
    public Object strategyThumbup(String sid, @UserInfoParam UserInfo userInfo){
        boolean ret = statisticsVORedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ret);
    }



}
